package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     *  添加床位
     *  @param bedDto
     *  @return
     * */
    @Override
    public int addBed(BedDto bedDto) {
        //TODO 校验重名床位
        try {
            Bed bed =BeanUtil.copyProperties(bedDto, Bed.class);
            //默认0未入住
            bed.setBedStatus(0);
            return bedMapper.addBed(bed);
        } catch (BaseException e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_DUPLICATE);
        }
    }

    /**
     *删除床位
     * @param id
     * @return
     * */
    @Override
    public int deleteBed(Long id) {

        //TODO 校验床位是否被占用
        if(bedMapper.readBed(id).getBedStatus() == 1){
            return 0;
        }
        return bedMapper.deleteBed(id);
    }

    /**
     * 更新床位
     * @param bedDto
     * @return
     * */
    @Override
    public int updateBed(BedDto bedDto) {
        return bedMapper.updateBed(BeanUtil.copyProperties(bedDto, Bed.class));
    }

    /**
     * 根据id查询床位
     * @param id
     * @return
     * */
    @Override
    public Bed readBed(Long id) {
        return bedMapper.readBed(id);
    }


}

